/**
 * 
 */
package edu.umd.clip.lm.tools;

import java.io.*;
import java.text.ParseException;
import java.util.Vector;

import edu.berkeley.nlp.util.Option;
import edu.umd.clip.lm.factors.*;
import edu.umd.clip.lm.model.*;
import edu.umd.clip.lm.nbest.*;
import edu.umd.clip.lm.nbest.NbestFormat.NbestFormatData;
import edu.umd.clip.lm.util.IO;

import edu.berkeley.nlp.util.*;
/**
 * @author Denis Filimonov <den@cs.umd.edu>
 *
 */
public class FLMFilterNBestLists {

	public static class Options {
        @Option(name = "-input", required = true, usage = "NBest lists input directory")
		public String input;
        @Option(name = "-output", required = true, usage = "Nbest lists output directory")
		public String output;
        @Option(name = "-config", usage = "XML config file")
		public String config;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
        OptionParser optParser = new OptionParser(Options.class);
        Options opts = (Options) optParser.parse(args, true);

        File inDir = new File(opts.input);
        if (!inDir.isDirectory()) {
        	System.err.println(opts.input + " is not a directory");
        	return;
        }
        
        File outDir = new File(opts.output);
        if (!outDir.isDirectory()) {
        	if (!outDir.mkdirs()) {
        		System.err.println("failed to create " + outDir);
        		return;
        	}
        }
        
		Experiment.initialize(opts.config);
		Experiment experiment = Experiment.getInstance();

		FactorTupleDescription tupleDesc = experiment.getTupleDescription();

		FLMInputParser parser = new FLMInputParser(tupleDesc);

		for(File inFile : inDir.listFiles()) {
			try {
				InputStream in = IO.getInputStream(inFile);
				NbestFile nbest = NbestFile.readFile(in);
				in.close();
				if (nbest != null && nbest.getItems() != null) {
					for(NbestFormatData data : nbest.getItems()) {
						long[] sentence = parser.parseSentence(data.getSentence());
						if (sentence.length > 0) {
							StringBuilder sb = new StringBuilder(FactorTuple.toStringNoNull(sentence[0])); 
							for(int i=1; i<sentence.length; ++i) {
								sb.append(' ');
								sb.append(FactorTuple.toStringNoNull(sentence[i]));
							}
							data.setSentence(sb.toString());
						}
					}
					nbest.writeFile(new FileOutputStream(new File(outDir,inFile.getName())));
				}
			} catch(ParseException e) {
				System.err.println("Failed to parse " + inFile.getName());
				e.printStackTrace(System.err);
			} catch(IOException e) {
				e.printStackTrace();
			}
		}
	}
}
